package com.shoesdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shoesdemo.BaseActivity;
import com.shoesdemo.R;

import me.jessyan.autosize.utils.ScreenUtils;

public class WelcomeTestActivity extends BaseActivity {

    private CountDownTimer mCountDownTimer;
    private ImageView mIv_logo;
    private RelativeLayout mLlImage;

    @Override
    public int getLayoutView() {
        return R.layout.activity_welcome_test;
    }

    @Override
    protected void initView() {
        actionBar.hide();
        mIv_logo = findViewById(R.id.iv_logo);
        mLlImage = findViewById(R.id.ll_image);
        final LinearLayout ivOne = findViewById(R.id.ll_one);
        final LinearLayout ivTwo = findViewById(R.id.ll_two);
        mIv_logo.post(new Runnable() {
            @Override
            public void run() {
                startLogoAnim(mIv_logo);
            }
        });
        mLlImage.post(new Runnable() {
            @Override
            public void run() {
                int[] screenSize = ScreenUtils.getScreenSize(WelcomeTestActivity.this);
                int with = screenSize[0];
                startTransAnim(ivOne, 0, -with, 0);
                startTransAnim(ivTwo, with, 0, 0);
            }
        });
    }

    private void startTransAnim(View view, int distanceXStart, int distanceXEnd, int delay) {
        TranslateAnimation animation = new TranslateAnimation(distanceXStart, distanceXEnd, 1.0f, 1.0f);
        animation.setDuration(1000);
        animation.setStartTime(delay);
        //给按钮添加 动画效果
        view.startAnimation(animation);
    }

    private void startLogoAnim(ImageView iv_logo) {
        // 创建透明动画效果 ，透 明度是0~1
        AlphaAnimation alpha = new AlphaAnimation(0.1f, 1.0f);
        //设置动画时间 长度，单位毫秒
        alpha.setDuration(2000);
        //给按钮添加 动画效果
        iv_logo.startAnimation(alpha);

        ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        );
        scaleAnimation.setDuration(2000);
        scaleAnimation.setInterpolator(new BounceInterpolator());
        iv_logo.startAnimation(scaleAnimation);
    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mCountDownTimer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long lastTimeStamp) {

            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(WelcomeTestActivity.this, TomorrowMainActivity.class);
                Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(WelcomeTestActivity.this, findViewById(R.id.iv_lancher), "share").toBundle();
                startActivity(intent, bundle);
                finish();
            }
        };
        mCountDownTimer.start();
    }
}
